
//
//  ZYQLoginViewController.m
//  小庆聊天室
//
//  Created by 张毓庆 on 13-12-8.
//  Copyright (c) 2013年 张毓庆. All rights reserved.
//

#import "ZYQLoginViewController.h"
#import "ZYQAppDelegate.h"
#import "ZYQLoginUser.h"
#import "NSString+Helper.h"

@interface ZYQLoginViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *hostNameText;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation ZYQLoginViewController

#pragma mark - AppDelegate 的助手方法
- (ZYQAppDelegate *)appDelegate
{
    return [[UIApplication sharedApplication] delegate];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 拉伸按钮的背景图片
    // 登录按钮
    UIImage *loginImage = [UIImage imageNamed:@"LoginGreenBigBtn"];
    loginImage  = [loginImage stretchableImageWithLeftCapWidth:loginImage.size.width * 0.5 topCapHeight:loginImage.size.height * 0.5];
    [_loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
    // 注册按钮
    UIImage *registerImage = [UIImage imageNamed:@"LoginwhiteBtn"];
    registerImage = [registerImage stretchableImageWithLeftCapWidth:registerImage.size.width * 0.5 topCapHeight:registerImage.size.height * 0.5];
    [_registerButton setBackgroundImage:registerImage forState:UIControlStateNormal];
    
    
    // 设置文本界面的初始值
    self.userNameText.text = [ZYQLoginUser sharedZYQLoginUser].userName;
    self.passwordText.text = [ZYQLoginUser sharedZYQLoginUser].password;
//    self.hostNameText.text = [ZYQLoginUser sharedZYQLoginUser].hostName;
    self.hostNameText.text = @"chouyuqingnopasokon.local";
    
    // 设置一开始进入系统就打开键盘
    if ([_userNameText.text isEmptyString])
    {
        [_userNameText becomeFirstResponder];
    }
    else
    {
        [_passwordText becomeFirstResponder];
    }
    
}

#pragma mark - textfield代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _userNameText)
    {
        [_passwordText becomeFirstResponder];
    }
    else if (textField == _passwordText  && [_hostNameText.text isEmptyString])
    {
        [_hostNameText becomeFirstResponder];
    }
    else
    {
        [self userLoginAndRegister:nil];
    }
    
    return YES;
}

#pragma mark - 用户登录和注册的方法
- (IBAction)userLoginAndRegister:(UIButton *)sender
{
    NSString *userName = [_userNameText.text trimString];
    NSString *password = _passwordText.text;
    NSString *hostName = [_hostNameText.text trimString];
    
    // 检查用户信息是否完整
    if ([userName isEmptyString] || [password isEmptyString] || [hostName isEmptyString])
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登录信息不完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        return;
    }
    
    // 将用户的信息写入偏好设置中
    [[ZYQLoginUser sharedZYQLoginUser] setUserName:userName];
    [[ZYQLoginUser sharedZYQLoginUser] setPassword:password];
    [[ZYQLoginUser sharedZYQLoginUser] setHostName:hostName];
    
    // 让appdelegate开始连接
    NSString *errorMessage = nil;
    
    if (sender.tag == 1)
    {
        [self appDelegate].isRegisterUser = YES;
        errorMessage = @"注册用户失败";
    }
    else
    {
        errorMessage = @"用户登录失败";
    }
    
    [[self appDelegate] connectWithSuccessBlock:nil failed:^{
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:errorMessage delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alter show];
        
        
        if (sender.tag == 1)
        {
            [_userNameText becomeFirstResponder];
        }
        else
        {
            [_passwordText setText:@""];
            [_passwordText becomeFirstResponder];
        }
    }];
    
}

@end
